<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.MatchString"
              clearable
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="tableSortRef"
      v-loading="listLoading"
      :border="border"
      :data="list"
      :height="height"
      :size="lineHeight"
      :stripe="stripe"
      @selection-change="setSelectRows"
    >
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :sortable="item.sortable"
        width="auto"
      >
        <template slot-scope="scope">
          <span>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="200"
      >
        <template slot-scope="scope">
          <!-- <el-button @click="handleEdit(scope.row)">编辑</el-button> -->
          <el-button text type="primary" @click="handleDetail(scope.row)">
            详情
          </el-button>
          <el-button type="danger" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>

      <template slot="empty">
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />

    <el-dialog
      :close-on-click-modal="false"
      :visible="dialogVisible"
      title="生成订单"
      width="60%"
      :before-close="handleClose"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="订单编号">
          <el-input v-model="form.ProduceOrderId" :rows="2" type="text" />
        </el-form-item>
        <el-form-item label="客户名称">
          <el-input v-model="form.Customer" :rows="2" type="text" />
        </el-form-item>
        <!-- <el-form-item label="下单日期">
          <el-date-picker
            v-model="form.OrderTime"
            type="date"
            label="请选择下单日期"
            placeholder="请选择下单日期"
            style="width: 100%"
          />
        </el-form-item> -->
        <el-form-item label="交付时间">
          <el-date-picker
            v-model="form.PayTime"
            type="date"
            label="请选择交付时间"
            placeholder="请选择交付时间"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="订单优先级">
          <el-input v-model="form.Priority" :rows="2" type="text" />
        </el-form-item>
        <el-form-item prop="ProductionInfo">
          <vab-form-table
            v-model="form.ProductionInfo"
            drag
            :row-template="rowTemplate"
            style="width: 100%"
          >
            <el-table-column
              align="center"
              label="产品编号"
              prop="ProducationId"
            >
              <template slot-scope="scope">
                <el-select
                  filterable
                  v-model="scope.row.ProductionId"
                  placeholder="选择产品编号"
                  @change="(e) => cChange(e, scope.$index)"
                >
                  <template v-for="(item, index) in ProductInfoData">
                    <el-option
                      :label="item.ProducationId"
                      :value="item.ProducationId"
                    />
                  </template>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="产品名称"
              prop="ProductionName"
            >
              <template slot-scope="scope">
                <el-select
                  v-model="scope.row.ProductionName"
                  placeholder="选择产品名称"
                >
                  <template v-for="(item, index) in ProductInfoData">
                    <el-option
                      :label="item.ProducationName"
                      :value="item.ProducationName"
                    />
                  </template>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column align="center" label="数量" prop="MachiningCount">
              <template slot-scope="scope">
                <el-input v-model="scope.row.MachiningCount" />
              </template>
            </el-table-column>
            <!-- <el-table-column
              align="center"
              label="完成数量"
              prop="CompleteCount"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.CompleteCount" readonly />
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              label="合格数量"
              prop="QualityCount"
            >
              <template slot-scope="scope">
                <el-input v-model="scope.row.QualityCount" readonly />
              </template>
            </el-table-column> -->
          </vab-form-table>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submit">确认</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog
      :close-on-click-modal="false"
      :visible="dialogVisible1"
      title="订单详情"
      width="60%"
      :before-close="beforeClose"
    >
      <div>
        <el-descriptions direction="vertical" :column="4" border>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              订单编号
            </template>
            {{ info.ProduceOrderId }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              客户名称
            </template>
            {{ info.Customer }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user"></i>
              下单日期
            </template>
            {{ info.OrderTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <vab-icon icon="time-line" />
              交付时间
            </template>
            {{ info.PayTime }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <vab-icon icon="time-line" />
              订单优先级
            </template>
            {{ info.Priority }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <vab-icon icon="time-line" />
              负责人
            </template>
            {{ info.Creator }}
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <vab-icon icon="time-line" />
              创建时间
            </template>
            {{ info.CreateTime }}
          </el-descriptions-item>
        </el-descriptions>

        <el-table
          ref="singleTableRef"
          :data="info.ProductionInfo"
          highlight-current-row
          style="width: 100%; margin-top: 20px"
          @current-change="handleCurrentChange"
        >
          <el-table-column type="index" width="50" />
          <el-table-column
            property="ProductionId"
            label="产品编号"
            width="120"
          />
          <el-table-column
            property="ProductionName"
            label="产品名称"
            width="120"
          />
          <el-table-column property="MachiningCount" label="数量" />
          <el-table-column property="CompleteCount" label="完成数量" />
          <el-table-column property="QualityCount" label="合格数量" />
        </el-table>
      </div>
      <template slot="footer">
        <span class="dialog-footer">
          <el-button @click="dialogVisible1 = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import VabFormTable from '@/extra/VabFormTable'
  import { mapGetters } from 'vuex'
  import {
    produceOrderList,
    ProduceOrder,
    ProduceOrderDel,
    OrderDetail,
    GetProduction,
  } from '@/api/production'
  export default {
    components: { VabFormTable },
    data() {
      return {
        rowTemplate: {
          ProductionId: '',
          ProductionName: '',
          MachiningCount: '',
          CompleteCount: '0',
          QualityCount: '0',
        },
        TrayInfoData: [],
        form: {
          ProductionId: '',
          Customer: '',
          OrderTime: '',
          PayTime: '',
          Priority: '',
          Creator: '',
          Creator: '',
          ProductionInfo: [],
        },
        radio: 3,
        formLabelWidth: '100px',
        dialogVisible: false,
        dialogVisible1: false,
        tableSortRef: null,
        editRef: null,
        border: true,
        height: this.$baseTableHeight(1),
        stripe: false,
        lineHeight: 'small',
        isFullscreen: false,
        checkList: [
          '订单编号',
          '客户名称',
          '下单日期',
          '交付日期',
          '订单优先级',
          '创建人',
          '创建时间',
        ],
        columns: [
          {
            label: '订单编号',
            prop: 'ProduceOrderId',

            disableCheck: true,
          },
          {
            label: '客户名称',
            prop: 'Customer',
          },
          {
            label: '下单日期',
            prop: 'CreateTime',
          },
          {
            label: '交付日期',
            prop: 'PayTime',
          },
          {
            label: '订单优先级',
            prop: 'Priority',
          },
          {
            label: '创建人',
            prop: 'Creator',
          },
          {
            label: '创建时间',
            prop: 'CreateTime',
          },
          {
            label: '不合格数量',
            prop: 'rate',
          },
          {
            label: '完成率',
            prop: 'rate',
          },
          {
            label: '创建者',
            prop: 'rate',
          },

          {
            label: '修改工单名',
            prop: 'rate',
          },
          {
            label: '流程图',
            prop: 'pageViews',
          },
          {
            label: '开始加工',
            prop: 'datetime',
          },
        ],
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          MatchString: '',
        },

        info: {},
        ProductInfoData: [],
      }
    },
    computed: {
      ...mapGetters({
        username: 'user/username',
      }),
      finallyColumns() {
        return this.columns.filter((item) =>
          this.checkList.includes(item.label)
        )
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      cChange(e, index) {
        console.log(this.ProductInfoData)
        console.log('rr', e)

        this.ProductInfoData.map((v, i) => {
          if (v.ProducationId == e) {
            console.log('MachiningCount', v.MachiningCount)
            this.form.ProductionInfo[index].ProductionName = v.ProducationName
            this.form.ProductionInfo[index].MachiningCount = v.MachiningCount
          }
        })
        console.log('this.form.ProductionInfo', this.form.ProductionInfo)

        // this.form.ProductionInfo.ProductionName = row.ProductionName
      },
      beforeClose() {
        this.dialogVisible1 = false
      },
      submit() {
        this.form.OrderTime = this.days()
        this.form.PayTime = this.days(this.form.PayTime)
        this.form.CreateTime = this.days()
        this.form.Creator = this.username
        ProduceOrder(this.form).then((res) => {
          console.log(res)
          this.dialogVisible = false
          this.fetchData()
        })
      },
      handleDetail(row) {
        this.info = row
        this.dialogVisible1 = true
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.ProduceOrderId) {
          this.form = row
        } else {
          // this.$refs['edit'].showEdit()
        }
        GetProduction().then((res) => {
          console.log('res', res)
          this.ProductInfoData = res.ProductInfo
        })
        this.dialogVisible = true
      },
      handleClose() {
        this.dialogVisible = false
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await ProduceOrderDel({
            ProduceOrderId: row.ProduceOrderId,
          })
          // $baseMessage(msg, 'success', 'vab-hey-message-success')
          await this.fetchData()
        })
        return false
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const params = {
          Index: this.getPage(this.queryForm.pageNo, this.queryForm.pageSize),
          Count: this.queryForm.pageSize,
          MatchString: this.queryForm.MatchString,
        }
        produceOrderList(params).then((res) => {
          this.list = res.OrderList
          this.total = res.TotalCount
          this.listLoading = false
        })
      },
    },
  }
</script>
